package com.qianfeng.es.service.impl;

//
//                            _ooOoo_
//                           o8888888o
//                           88" . "88
//                           (| -_- |)
//                            O\ = /O
//                        ____/`---'\____
//                      .   ' \\| |// `.
//                       / \\||| : |||// \
//                     / _||||| -:- |||||- \
//                       | | \\\ - /// | |
//                     | \_| ''\---/'' | |
//                      \ .-\__ `-` ___/-. /
//                   ___`. .' /--.--\ `. . __
//                ."" '< `.___\_<|>_/___.' >'"".
//               | | : `- \`.;`\ _ /`;.`/ - ` : | |
//                 \ \ `-. \_ __\ /__ _/ .-` / /
//         ======`-.____`-.___\_____/___.-`____.-'======
//                            `=---='
//
//         .............................................
//                  佛祖镇楼                  BUG辟易
//          佛曰:
//                  写字楼里写字间，写字间里程序员；
//                  程序人员写程序，又拿程序换酒钱。
//                  酒醒只在网上坐，酒醉还来网下眠；
//                  酒醉酒醒日复日，网上网下年复年。
//                  但愿老死电脑间，不愿鞠躬老板前；
//                  奔驰宝马贵者趣，公交自行程序员。
//                  别人笑我忒疯癫，我笑自己命太贱；
//


import com.qianfeng.es.service.IndexService;
import com.qianfeng.es.utils.ConnectionUtils;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.client.indices.GetIndexResponse;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.json.JsonXContent;

/**
 * Created by Jackiechan on 2022/10/12 11:13
 *
 * @author Jackiechan
 * @version 1.0
 * @since 1.0
 */
public class IndexServiceImpl implements IndexService {

    @Override
    public void createIndex(String index) throws Exception {

        if (exist(index)) {
            //true代表存在,存在就不创建
            System.err.println("index" + index + "已经存在了");
            return;
        }

        //二话不说,先获取连接
        RestHighLevelClient client = ConnectionUtils.getClient();
        //创建库的请求对象,设置我们的index名字
        CreateIndexRequest request = new CreateIndexRequest(index);

        //设置分片和列
        Settings settings = Settings.builder()
                .put("number_of_shards", 3).put("number_of_replicas", 1)
                .build();
        //设置列
        XContentBuilder xContentBuilder = JsonXContent.contentBuilder()
                .startObject()
                .startObject("properties")
                .startObject("corpName").field("type", "keyword").endObject()
                .startObject("longCode").field("type", "keyword").endObject()
                .startObject("mobile").field("type", "keyword").endObject()
                .startObject("province").field("type", "keyword").endObject()
                .startObject("fee").field("type", "integer").endObject()
                .startObject("operatorId").field("type", "integer").endObject()
                .startObject("replyTotal").field("type", "integer").endObject()
                .startObject("state").field("type", "integer").endObject()
                .startObject("ipAddr").field("type", "ip").endObject()
                .startObject("createDate").field("type", "date").field("format", "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis").endObject()
                .startObject("sendDate").field("type", "date").field("format", "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis").endObject()
                .startObject("smsContent").field("type", "text").field("analyzer", "ik_max_word").endObject()
                .endObject()
                .endObject();
        //给请求设置分片和列的数据
        request.settings(settings)
                .mapping(xContentBuilder);

        //创建库
        CreateIndexResponse response = client.indices()//这里面的都是和index相关的操作
                .create(request, RequestOptions.DEFAULT);
        System.err.println(response.isAcknowledged());
    }

    @Override
    public boolean exist(String index) throws Exception {
        RestHighLevelClient client = ConnectionUtils.getClient();
        return client.indices().exists(new GetIndexRequest(index), RequestOptions.DEFAULT);
    }

    @Override
    public void getIndex(String index) throws Exception {
        RestHighLevelClient client = ConnectionUtils.getClient();
        GetIndexResponse response = client.indices().get(new GetIndexRequest(index), RequestOptions.DEFAULT);
        System.err.println(response.getMappings().get("sms-logs-index").getSourceAsMap());
    }

    @Override
    public void deleteIndex(String index) throws Exception {
        RestHighLevelClient client = ConnectionUtils.getClient();
        AcknowledgedResponse response = client.indices().delete(new DeleteIndexRequest(index), RequestOptions.DEFAULT);
        System.err.println(response.isAcknowledged());
    }
}
